package cn.wzx.controller;

import cn.wzx.entity.Largefile;
import cn.wzx.entity.MyUser;
import cn.wzx.respJson.RespJson;
import cn.wzx.service.LargefileService;
import cn.wzx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@CrossOrigin // 允许跨域访问
@Controller
@RequestMapping("/api/adm/user")
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    LargefileService largefileService;

    // 用户全查询并且有分页功能
    @ResponseBody
    @RequestMapping({"/findAll/{start}/{num}"})
    public Map<String, Object> findAll(@PathVariable("start") Integer start, @PathVariable("num") Integer num) {
        System.out.println("start===========" + start);
        System.out.println("num===========" + num);
        Map<String, Object> map = new HashMap<>();
        // 总页数和分页功能
        map.put("total", userService.count());
        map.put("users", userService.findAll(start-1, num));
        return RespJson.pack(200, "", map);
    }

    // 添加管理员，默认添加个G，用于管理员添加
    @ResponseBody
    @RequestMapping({"/addAdm"})
    public Map<String, Object> addUser(MyUser user, Largefile largefile, @RequestParam(value = "pic",required = false) MultipartFile file) {
        System.out.println("file============" + file);
        if (file == null) {
            return RespJson.pack(500,"图片未上传",null);
        }
        if (file != null && file.getSize()>0){
            try {
                String uuid = UUID.randomUUID().toString();
                user.setImg(uuid);
                largefile.setId(uuid);
                largefile.setFilename(file.getOriginalFilename());
                largefile.setContent(file.getBytes());
                largefileService.add(largefile);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        // 默认添加个G，用于管理员添加
        user.setRole("G");
        user.setClosed(0);
        userService.addUser(user);
        return RespJson.pack(200, "添加管理员成功",null);
    }

    // 根据用户ID修改密码
    @ResponseBody
    @RequestMapping({"/resetPwd/{uid}"})
    public Map<String, Object> resetPwd(@PathVariable("uid") int uid) {
        System.out.println("uid==========" + uid);
        Map<String, Object> map = new HashMap<>();
        map.put("id", uid);
        // 重置密码: 111111, 也将解冻这个账号
        map.put("password", "111111");
        map.put("closed", 0);
        userService.update(map);
        return RespJson.pack(200,"密码重置成功","");
    }

    //根据用户ID冻结账号
    @ResponseBody
    @RequestMapping({"/close/{uid}"})
    public Map<String, Object> close(@PathVariable("uid") int uid) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", uid);
        map.put("closed", 1);
        userService.update(map);
        return RespJson.pack(200,"冻结成功","");
    }

    // 根据ID进行查询
    @ResponseBody
    @RequestMapping({"/findById/{id}"})
    public Map<String, Object> findById(@PathVariable("id") int id) {
        System.out.println("id===============" + id);
        MyUser user = userService.findById(id);
        return RespJson.pack(200, "", user);
    }
}
